﻿Imports CodeMatrix.Context
Imports CodeMatrix.Entities
Imports System.Linq
Imports System.Data
Imports System.IO
Imports System.Data.SqlClient

Partial Class Apps_ViewSystem
    Inherits System.Web.UI.Page

    Private _menuSystem As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Try

            Dim tblAuthenMenuSystem = CodeMatrix.Utilities.CodeHelper.GetDataTable("exec USP_GetMenuSystemByUserPrivilege @staff_code, @main_system_code", _
                                                                   New SqlParameter("@staff_code", HttpContext.Current.Session("staff_code")), _
                                                                   New SqlParameter("@main_system_code", Request.QueryString("Edit")))

            Dim tblMasterType = CodeMatrix.Utilities.CodeHelper.GetDataTable("exec USP_List_master_type @type_status", New SqlParameter("@type_status", 1))
            Dim tblMenuSystem = CodeMatrix.Utilities.CodeHelper.GetDataTable("exec USP_GetMenuSystem @main_system_code", New SqlParameter("@main_system_code", Request.QueryString("Edit")))

            If (tblMasterType.Rows.Count > 0) Then

                ' Start Generate table
                ltrMenuSystemTable.Text = "<table id='idMenuSystemTable' cellspacing=""2"" cellpadding=""2"" bordercolor=""#999999"" border=""0"" align=""center"" width=""100%"">"

                Dim colEmpty As String = "<td bgcolor=""#D8D8D8"" class=""td_data"">&nbsp;</td>"
                Dim rowEmpty As String = ""

                ' Generate Header
                ltrMenuSystemTable.Text &= "<tbody><tr bgcolor=""#ff9999"" align=""center"" ><td class=""th_data"" colspan=""2"">ระบบ/เมนูระบบ</td>"
                Dim i As Int32 = 0
                Dim j As Int32 = 0
                Dim k As Int32 = 0
                For i = 0 To tblMasterType.Rows.Count - 1 Step 1
                    ltrMenuSystemTable.Text &= String.Format("<td class=""th_data"">{0}</td>", tblMasterType.Rows(i)("type_name").ToString())
                    rowEmpty &= colEmpty
                Next
                ltrMenuSystemTable.Text &= "</tr>"

                If (tblAuthenMenuSystem.Rows.Count > 0) Then
                    Dim main_system_code As Int32 = -1
                    For i = 0 To tblMenuSystem.Rows.Count - 1 Step 1
                        Dim rowMenu As DataRow = tblMenuSystem.Rows(i)
                        Dim rowAuthenMenu As DataRow() = tblAuthenMenuSystem.Select("menu_system_code = " & rowMenu("menu_system_code"))
                        If rowAuthenMenu.Count() > 0 Then
                            ' Main System
                            If main_system_code <> rowMenu("main_system_code") Then
                                ltrMenuSystemTable.Text &= String.Format("<tr><td bgcolor=""#D8D8D8"" colspan=""2"" class=""td_data"">{0}</td>{1}</tr>", rowMenu("main_system_name").ToString(), rowEmpty)
                                _menuSystem = rowMenu("main_system_name").ToString()
                            End If
                            ltrMenuSystemTable.Text &= String.Format("<tr><td colspan=""2"" class=""td_data"">{0}</td>", rowAuthenMenu(0)("menu_system_name").ToString())
                            For k = 0 To tblMasterType.Rows.Count - 1 Step 1
                                Dim rowMasterType As DataRow = tblMasterType.Rows(k)
                                Dim rowMenuType As DataRow() = tblAuthenMenuSystem.Select("menu_system_code = " & rowMenu("menu_system_code") & " and type_code = " & rowMasterType("type_code"))
                                If rowMenuType.Count() > 0 Then
                                    If rowMenuType(0)("has_authen") Then
                                        If rowMenuType(0)("main_system_type") = 2 Then
                                            ltrMenuSystemTable.Text &= String.Format("<td align='center' class='td_data' bordercolor='#CCCCCC'>[&nbsp;<a href='iframe.aspx?url={0}{1}'>คลิ๊ก</a>&nbsp;]&nbsp;</td>", rowMenuType(0)("menu_system_url"), rowMenuType(0)("sub_menu_url"))
                                        ElseIf rowMenuType(0)("main_system_type") = 1 Then
                                            ltrMenuSystemTable.Text &= String.Format("<td align='center' class='td_data' bordercolor='#CCCCCC'>[&nbsp;<a href=""{0}"">คลิ๊ก</a>&nbsp;]&nbsp;</td>", rowMenuType(0)("sub_menu_url"))
                                        End If
                                    Else
                                        ltrMenuSystemTable.Text &= String.Format("<td align='center' class='td_data' bordercolor='#CCCCCC'>&nbsp;</td>")
                                    End If
                                Else
                                    ltrMenuSystemTable.Text &= String.Format("<td align='center' class='td_data' bordercolor='#CCCCCC'>&nbsp;</td>")
                                End If
                            Next
                            ltrMenuSystemTable.Text &= "</tr>"
                            main_system_code = rowMenu("main_system_code")
                        End If
                    Next
                    ltrMenuSystemTable.Text &= String.Format("<tr><td align=""center"" bgcolor=""#ffffff"" class=""td_data_end"" colspan=""{0}""><center><input type=""button"" onclick=""window.location = 'welcome.aspx';"" value=""กลับหน้าแรก"" /></center></td></tr>", tblMasterType.Rows.Count + 2)
                End If

                ltrMenuSystemTable.Text &= "</tbody></table>"

            End If
        Catch ex As Exception
            GenericHelper.ShowAlert(Me, ex.Message)
        End Try

    End Sub

    Protected Sub Page_LoadComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LoadComplete
        Dim BreadCrumpBG As HtmlControl = Page.Master.FindControl("tdBreadCrump")
        Dim BreadCrump As Literal = Page.Master.FindControl("ltrBreadCrump")
        CodeMatrix.Business.GenericData.SetBreadCrump(BreadCrumpBG, BreadCrump, _menuSystem)
    End Sub
End Class

